package com.taomeng.servlet.admin;

import com.taomeng.entity.Cls;
import com.taomeng.entity.ClsInfo;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.RespBody;
import com.taomeng.service.ClassService;
import com.taomeng.servlet.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/cls/*")
public class ClsServlet extends BaseServlet {
    @Autowired
    private ClassService classService;

    public void add(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();

        Cls cls = new Cls();
        BeanUtils.populate(cls, map);
        classService.add(cls);

        RespBody respBody = new RespBody();
        respBody.setCode(200);
        respBody.setMsg("新增班级成功");
        RespBody.JsonObject(resp, respBody);
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        classService.del(id);

        RespBody respBody = new RespBody();
        respBody.setCode(200);
        respBody.setMsg("删除成功");

        RespBody.JsonObject(resp, respBody);
    }

    public void findAllInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int page = Integer.parseInt(req.getParameter("page"));
        int limit = Integer.parseInt(req.getParameter("limit"));
        String key = req.getParameter("key");
        PageBean<ClsInfo> pageBean = classService.findByPage(page, limit, key);

        RespBody respBody = new RespBody();
        respBody.setCode(0);
        respBody.setCount((long) pageBean.getList().size());
        respBody.setData(pageBean.getList());
        RespBody.JsonObject(resp, respBody);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Cls> clsList = classService.findAll();


        RespBody respBody = new RespBody();
        respBody.setCode(200);
        respBody.setMsg("");
        respBody.setCount((long) clsList.size());
        respBody.setData(clsList);

        RespBody.JsonObject(resp, respBody);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = req.getParameterMap();

        Cls cls = new Cls();
        BeanUtils.populate(cls, map);

        RespBody respBody = new RespBody();
        if (classService.update(cls)) {
            respBody.setCode(200);
            respBody.setMsg("修改成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("修改失败");
        }
        RespBody.JsonObject(resp, respBody);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext application = this.getServletContext();
        // 解决servlet用@Autowired自动注入service失败的问题
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, application);
    }
}
